package com.MoveEat.views;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import com.MoveEat.R;
import com.MoveEat.Utils.Utils;
import com.MoveEat.activities.adapters.MenuDataAdapter;
import com.MoveEat.items.DishDataItem;
import com.google.gson.Gson;

public class MenuHandler
{

	private long						mId;
	private String						mRestName;
	private Context						mContext;
	private boolean						finishedLoading;
	private View						menuLayout;
	private HashMap<Integer, String>	mMenuCats;
	private long						mMenuID;

	public MenuHandler(long restId, Context context, String restName, String menuName, long menuId, HashMap<Integer, String> menuCats)
	{
		mId = restId;
		mRestName = restName;
		mContext = context;
		mMenuCats = menuCats;
		mMenuID = menuId;
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		menuLayout = inflater.inflate(R.layout.menu_layout, null);
		getMenuList();
	}

	private void getMenuList()
	{
		new AsyncTask<Void, String, String>()
		{
			@Override
			protected void onPreExecute()
			{
			}

			@Override
			protected String doInBackground(Void... arg0)
			{
				return (Utils.preformRequest(Utils.BASE_URL + "GetMenus.php?rest_id=" + mId + "&menuId=" + mMenuID));
			}

			@Override
			protected void onPostExecute(String body)
			{
				Utils.log(2,body);
				if (body != null)
				{
					JSONObject obj = null;
					try
					{
						obj = new JSONObject(body);
					}
					catch (JSONException e)
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (obj != null)
					{
						JSONArray arr = obj.optJSONArray("dishes");
						displayMenuDetails(arr);
					}
					else
					{

					}
				}
			}

		}.execute();

	}

	protected void displayMenuDetails(JSONArray arr)
	{
		ArrayList<DishDataItem> dishesList = new ArrayList<DishDataItem>();
		Gson gson = new Gson();
		int lastCat = -1;
		for (int j = 0; j < arr.length(); j++)
		{
			DishDataItem currObj = gson.fromJson(arr.optJSONObject(j).toString(), DishDataItem.class);
			if (currObj.getDISH_CAT_ID() != lastCat)
			{
				DishDataItem cat = new DishDataItem();
				cat.setIsCat(true);
				cat.setCatName(mMenuCats.get(currObj.getDISH_CAT_ID()));
				dishesList.add(cat);
				lastCat = currObj.getDISH_CAT_ID();
			}
			dishesList.add(currObj);
		}

		ListView list = (ListView) menuLayout.findViewById(R.id.menu_list);
		list.setFocusableInTouchMode(false);
		MenuDataAdapter menuAdapter = new MenuDataAdapter(dishesList, mContext);
		list.setAdapter(menuAdapter);
		finishedLoading = true;
	}

	public boolean isFinished()
	{
		return finishedLoading;
	}

	public View getView()
	{
		return menuLayout;
	}

}
